from flask import Flask, request, redirect, url_for, render_template
import os
import subprocess

app = Flask(__name__)

# 设置允许上传的文件扩展名
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'}

# 检查文件扩展名是否合法
def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS

# 上传文件的路由
@app.route('/upload', methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        # 检查是否有文件在请求中
        if 'file' not in request.files:
            return redirect(request.url)
        file = request.files['file']
        # 如果用户没有选择文件，浏览器也会提交一个空的文件
        if file.filename == '':
            return redirect(request.url)
        if file and allowed_file(file.filename):
            #filename = secure_filename(file.filename)
            # 保存文件
            #filename = os.path.join('/uploads/yzm2.png')
            filename = '/u01/temp/yzm/uploads/yzm3.png'
            print(filename)
            file.save(filename)
            cmd=['ollama','run','minicpm-v','"read text in picture,return the text only"',filename]
            result = subprocess.run( cmd,stdout=subprocess.PIPE )
            return result.stdout.replace(b"\n",b"")
    return render_template('upload.html')

# 运行Flask应用
if __name__ == '__main__':
    app.run(debug=True,port=5555,host="0.0.0.0")

